import { Router } from "express";
import swaggerUI from "swagger-ui-express";
import swaggerJsdoc from "swagger-jsdoc";
import path from "path";
import myConfig from "../config";

const { enableReverseProxy } = myConfig;

const apis = [path.join(__dirname, "transactions.js")];
if (enableReverseProxy) {
    apis.push(path.join(__dirname, "proxyToRepChain.js"));
}

const options = {
    swaggerDefinition: {
        openapi: "3.0.0",
        info: {
            title: "repchain-tx-proxy-service API",
            version: "2.0.0",
            description: "A web service as proxy to generate and submit transactions to RepChain for the clients",
            license: {
                name: "Apache-2.0",
                url: "https://apache.org/licenses/LICENSE-2.0.html",
            },
        },
    },
    apis,
};
const specs = swaggerJsdoc(options);

const router = Router();

router.use("/", swaggerUI.serve);
router.get("/", swaggerUI.setup(null, {
    swaggerOptions: {
        url: "/apiDocs/swagger.json",
    },
    explorer: true,
}));
router.get("/swagger.json", (req, res) => {
    res.json(specs);
});

export default router;
